package jp.co.cos.ittrick.channel.controller.client;

import jp.co.cos.ittrick.channel.model.ChapterModel;
import jp.co.cos.ittrick.channel.service.ChapterService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class ChangeChapterNameController extends Controller {

    private ChapterService chapterService;

    public ChangeChapterNameController() {
        chapterService = new ChapterService();
    }

    @Override
    public Navigation run() throws Exception {

        // リクエストパラメータ
        String chapter = asString("chapter");
        String name = asString("name");

        // Modelの設定
        ChapterModel model = chapterService.getChapterByKey(Integer.valueOf(chapter));
        if(model != null) {
            model.setName(name);

            // Insert
            chapterService.update(model);
        }

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        response.getWriter().println("0");
        response.flushBuffer();

        return null;
    }
}
